package com.qqqq.web;

import com.qqqq.core.AjaxResult;
import com.qqqq.core.BaseHttpServlet;
import com.qqqq.dao.impl.StudentDaoImpl;
import com.qqqq.domain.Student;
import com.qqqq.service.StudentService;
import com.qqqq.service.impl.StudentServiceImpl;
import com.qqqq.utils.RequestUtils;
import com.qqqq.utils.ResponseUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/update")
public class UpdateStuServlet extends BaseHttpServlet {
    private StudentService studentService = new StudentServiceImpl(new StudentDaoImpl());

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Student student = RequestUtils.getReqJsonBody(req, Student.class);
        if (student.getId() == null) {
            ResponseUtils.responseJson(resp, AjaxResult.error(400, "请传入id"));
            return;
        }
        executeBusiness(resp, response -> {
            ResponseUtils.responseJson(resp, AjaxResult.success(studentService.updateStudent(student)));
        });

    }
}
